package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ShakeService;
import net.sf.json.JSONObject;

/**
 * 摇一摇(app)
 * @author admin101
 *
 */
@SuppressWarnings("serial")
public class ShakeAction extends AppBaseAction{	

	@Resource(name = ShakeService.BEAN_NAME)
	ShakeService shakeService;

	/**
	 * 摇积分
	 * @return
	 */
	public String getScore(){

		JSONObject jsonObject = null;
		try {
			
			this.validateSign(getLoginSign(),request.getParameter("userId"));
			this.validateLoginApp();
			jsonObject = this.shakeService.getScore(request.getParameter("userId"));
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机摇积分");//记录操作日志
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);

	}
	
	
	/**
	 * 摇一摇日志
	 * @return
	 */
	public String getLogs(){
		JSONObject jsonObject = null;
		try{
			
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String userId = this.getUserId();
			
			this.validateSign(getLoginSign(),getUserId());//验证签名:loginSign+userId
			this.validateLoginApp();//验证是否登录
			
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
			jsonObject = this.shakeService.getLogs(userId,page);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

}
